#include <stdio.h>
#include <stdlib.h>

typedef struct DNode
{
	int freq;
	char data;
	struct DNode* prev;
	struct DNode* next;
} DNode, *PDNode;

PDNode head;

void swap(PDNode p)
{
	char temp_data;
	int temp_freq;

	PDNode pre = p->prev;

	while (pre != NULL && pre->freq < p->freq)
	{
		temp_data = p->data;
		temp_freq = p->freq;

		p->data = pre->data;
		p->freq = pre->freq;

		pre->data = temp_data;
		pre->freq = temp_freq;

		p = pre;
		pre = pre->prev;
	}
}

void Search(char tem)
{
	PDNode ahead = head;

	while (ahead != NULL)
	{
		if (tem == ahead->data)
		{
			ahead->freq++;
			swap(ahead);
		}
		ahead = ahead->next;
	}
}

void Insert_tail(char val)
{
	PDNode ahead = head;
	PDNode temp;
	temp = (PDNode)malloc(sizeof(DNode));
	temp->freq = 0;
	temp->data = val;
	temp->next = NULL;
	temp->prev = NULL;

	if (ahead == NULL)
	{
		head = temp;
		return;
	}
	while (ahead->next != NULL)
	{
		ahead = ahead->next;
	}
	ahead->next = temp;
	temp->prev = ahead;
}

void Print()
{
	PDNode temp = head;

	while (temp != NULL)
	{
		if (temp->next != NULL)
			printf("%c ", temp->data);
		else
			printf("%c", temp->data);

		temp = temp->next;
	}
}

int main()
{
	head = NULL;
	int m, n;
	scanf("%d%d", &m, &n);


	char val;
	char word, word1;
	for (int i = 0; i < m; i++)
	{
		scanf(" %c", &val);

		Insert_tail(val);
	}

	for (int j = 0; j < n; j++)
	{
		scanf(" %c", &word1);
		if (word1 != '\n')
		{
			Search(word1);
		}
	}

	Print();
	return 0;
}
